import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class NodeInterface extends JLayeredPane{
	
	private static final long serialVersionUID = 1L;
	boolean showPets = false;
	InterfaceFrame frame;
	JPopupMenu popPrime = new JPopupMenu();
	JPanel popOptions = new JPanel();
	JTextArea textArea = new JTextArea();
	JTextArea initImageArea = new JTextArea();
	JTextArea initOptionsArea = new JTextArea();
	JLabel imageLabel = new JLabel();
	JLabel background = new JLabel();
	JMenuItem quest;
	JMenuItem questLog;
	JMenuItem trouble;
	JMenuItem trainer;
	JMenuItem shop;
	JMenuItem backPack;
	JMenuItem pets;
	JMenuItem abilities;
	JMenuItem map;
	JMenuItem save;
	JMenuItem exit;
	JMenuItem lastItem = new JMenuItem();
	Font font = new Font("Verdana", Font.PLAIN, 25);
	Font font2 = new Font("Verdana", Font.PLAIN, 15);
	Color textColor;
	boolean twice = false;
	boolean hasQuestLog = false;
	MapNode node;
	Player player;
	NodeInventory inventory;
	NodeShopInterface shopInt;
	NodeSpellbook spellBookInt;
	NodePet petsInt;
	NodeQuestInterface nodeQuest;
	NodeQuestLog nodeQuestLog;
	NodeTrainer nodeTrainer;
	NodeSave nodeSave;
	NodeExit nodeExit;
	
	NodeInterface(MapNode node, Player player, InterfaceFrame frame, boolean showPets){
		
		this.showPets = showPets;
		this.node = node;
		this.player = player;
		this.frame = frame;
		inventory = new NodeInventory(this.player, this.frame);
		shopInt= new NodeShopInterface(this.player, this.frame);
		petsInt = new NodePet(this.player, this.frame);
		spellBookInt=new NodeSpellbook(this.player,this.frame);
		nodeTrainer = new NodeTrainer(this.player, this.frame, this.node);
		nodeSave = new NodeSave(this.player, this.frame);
		nodeExit = new NodeExit(this.player, this.frame);
		init();
		
	}
	
	public void init(){

		UIManager.put("MenuItem.selectionBackground", new Color(0,0,255,50));
		UIManager.put("MenuItem.selectionForeground", Color.BLUE);
		setLayout(null);
		
		textArea.setBackground(new Color(0,0,0,50));
		
		if(node.type.equals(TerrainType.City)){
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/city.png")), JLabel.LEADING);
			popOptions.setBackground(Color.GRAY);
			textArea.setForeground(Color.BLACK);
		}
		else if(node.type.equals(TerrainType.Village)){
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/village.png")), JLabel.LEADING);
			popOptions.setBackground(new Color(176,224,230));
			textArea.setForeground(Color.BLACK);
		}
		else if(node.type.equals(TerrainType.Forest)){
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/thisShit.png")), JLabel.LEADING);
			UIManager.put("MenuItem.foreground", Color.WHITE);
			popOptions.setBackground(new Color(107,142,35));
			textArea.setForeground(Color.BLACK);
		}
		else if(node.type.equals(TerrainType.Field)){
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/field.png")), JLabel.LEADING);
			popOptions.setBackground(new Color(144,238,144));
			textArea.setForeground(Color.BLACK);
		}
		else if(node.type.equals(TerrainType.Mountains)){
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/mounatins.png")), JLabel.LEADING);
			popOptions.setBackground(Color.GRAY);
			textArea.setForeground(Color.BLACK);
		}
		else if(node.type.equals(TerrainType.Sand)){
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/beach.gif")), JLabel.LEADING);
			popOptions.setBackground(new Color(176,224,230));
			textArea.setForeground(Color.BLACK);
		}
		else if(node.type.equals(TerrainType.Lake)){
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/lake.png")), JLabel.LEADING);
			popOptions.setBackground(new Color(176,224,230));
			textArea.setForeground(Color.BLACK);
		}
		else{
			background = new JLabel("", new ImageIcon(getClass().getResource("/Pics/field.png")), JLabel.LEADING);
			popOptions.setBackground(new Color(144,238,144));
			textArea.setForeground(Color.BLACK);
		}
		/*
		addQuestLogIfNeeded();
		
		if(node.activeQuestIndex != -1){

			nodeQuest = new NodeQuestInterface(this.player,this.frame, node);
			quest = new JMenuItem("Uppdrag");
			popPrime.add(quest);
			quest.setOpaque(false);
			quest.setFont(font);
			quest.addActionListener(new ButtonListener());
			quest.addMouseListener(new TheMouseListener());
			
		}
		*/
		if(node.shop){
			shop = new JMenuItem("Affär");
			popPrime.add(shop);
			shop.setOpaque(false);
			shop.setFont(font);
			shop.addActionListener(new ButtonListener());
			shop.addMouseListener(new TheMouseListener());
		}
		if (node.getTrainer(player) != null) {
			trainer = new JMenuItem("Möt tränare");
			if(node.distance == 1)
				trainer.setText("Möt gymledare");
			trainer.setOpaque(false);
			trainer.setFont(font);
			popPrime.add(trainer);
			trainer.addActionListener(new ButtonListener());
			trainer.addMouseListener(new TheMouseListener());
		}
		
		backPack = new JMenuItem("Ryggsäck");
		map = new JMenuItem("Karta");
		save = new JMenuItem("Spara");
		trouble = new JMenuItem("Leta efter ofog");
		exit = new JMenuItem("Exit");
		pets = new JMenuItem("Husdjur");
		abilities = new JMenuItem("Förmågor");
		
		popPrime.setBounds(50, 30, 325, 350);
		initOptionsArea.setBounds(825, 30, 306, 350);
		textArea.setBounds(400, 400, 730, 200);
		imageLabel.setBounds(400, 30, 400, 350);
		initImageArea.setBounds(400, 30, 400, 350);
		background.setBounds(0, 0, 1200, 676);		
		popOptions.setBounds(825, 30, 306, 350);
		
		popPrime.add(trouble);
		
		popPrime.add(backPack);
		popPrime.add(pets);
		popPrime.add(abilities);
		popPrime.add(map);
		popPrime.add(save);
		popPrime.add(exit);
		
		popPrime.setOpaque(false);
		exit.setOpaque(false);
		trouble.setOpaque(false);
		map.setOpaque(false);
		save.setOpaque(false);
		backPack.setOpaque(false);
		pets.setOpaque(false);
		abilities.setOpaque(false);
		
		popPrime.setPopupSize(325, 570);
		
		backPack.setFont(font);
		map.setFont(font);
		save.setFont(font);
		exit.setFont(font);
		trouble.setFont(font);
		map.setFont(font);
		pets.setFont(font);
		abilities.setFont(font);
		textArea.setFont(font2);
		
		textArea.setEditable(false);
		initImageArea.setBackground(new Color(0, 0, 0, 50));
		initImageArea.setEditable(false);
		initOptionsArea.setBackground(new Color(0, 0, 0, 50));
		initOptionsArea.setEditable(false);
		
		add(background, new Integer(-1));
		add(popPrime, new Integer(1));
		add(textArea, new Integer(1));
		add(imageLabel, new Integer(2));
		add(initImageArea,new Integer(0));
		add(initOptionsArea,new Integer(0));
		
		popPrime.show(frame, 50, 55);
		pets.addActionListener(new ButtonListener());
		map.addActionListener(new ButtonListener());
		save.addActionListener(new ButtonListener());
		exit.addActionListener(new ButtonListener());
		trouble.addActionListener(new ButtonListener());
		backPack.addActionListener(new ButtonListener());
		abilities.addActionListener(new ButtonListener());
		abilities.addMouseListener(new TheMouseListener());
		pets.addMouseListener(new TheMouseListener());
		map.addMouseListener(new TheMouseListener());
		backPack.addMouseListener(new TheMouseListener());
		exit.addMouseListener(new TheMouseListener());
		trouble.addMouseListener(new TheMouseListener());
		background.addMouseListener(new TheMouseListener());
		initImageArea.addMouseListener(new TheMouseListener());
		initOptionsArea.addMouseListener(new TheMouseListener());
		imageLabel.addMouseListener(new TheMouseListener());
		textArea.addMouseListener(new TheMouseListener());
		background.addMouseListener(new TheMouseListener());
		frame.getContentPane().repaint();
		
		textColor = exit.getForeground();
		addKeyBindings();
		if(showPets){
			EventQueue.invokeLater(new Runnable() {
	            public void run() {
	            	pets.doClick();
	            }
	        });
		}
			
	}
	
	private void changeDisplay(Object type){
		
		// TODO: denna ger nullpointerexception någon gång (?)
		/*
		 * Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at NodeInterface.changeDisplay(NodeInterface.java:228)
	at NodeInterface.access$0(NodeInterface.java:226)
	at NodeInterface$ButtonListener.actionPerformed(NodeInterface.java:396)
		 */
		if(type.getClass().getName().equals("NodeInventory")){
			textArea.setText("");
			imageLabel.setIcon(null);
			popOptions.removeAll();
			revalidate();
			inventory.showInventoryLevel(0);
			imageLabel.setIcon(inventory.getPic());
			imageLabel.setBounds(400, 30, 400, 350);
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();			
		}
		else if(type.getClass().getName().equals("NodeShopInterface")){
			textArea.setText("");
			imageLabel.setIcon(null);
			popOptions.removeAll();
			revalidate();
			shopInt.getBuySellPanel();
			shopInt.getShopImage("questPics/oldManQuest.gif");
			imageLabel.setBounds(475, 30, 400, 350);
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();
		}
		

		else if(type.getClass().getName().equals("NodePet")){
			textArea.setText("");
			imageLabel.setIcon(null);
			// Hotfix
			if(!twice)
				twice = true;
			else{
				twice = false;
				backPack.doClick();
				pets.doClick();
			}
			popOptions.removeAll();
			revalidate();
			imageLabel.setIcon(petsInt.getPic());
			imageLabel.setBounds(475, 30, 400, 350);
			petsInt.passivePet.setForeground(Color.BLACK);
			petsInt.activePet.setForeground(Color.BLACK);
			add(popOptions, new Integer(2));
			petsInt.setButtons();
			frame.getContentPane().repaint();
		}
		else if(type.getClass().getName().equals("NodeSpellbook")){
			textArea.setText("");
			imageLabel.setIcon(null);
			popOptions.removeAll();
			revalidate();
			spellBookInt.showInventoryLevel(0);
			imageLabel.setIcon(spellBookInt.getPic());
			imageLabel.setBounds(400, 30, 400, 350);
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();
		}/*
		else if(type.getClass().getName().equals("NodeQuestInterface")){
			//textArea.setText(nodeQuest.quest.dialog);
			popOptions.removeAll();
			imageLabel.setIcon(null);
			revalidate();
			//imageLabel.setIcon(nodeQuest.getQuestPic());
			imageLabel.setBounds(400, 30, 400, 350);
			//nodeQuest.showQuestOptions();
			//nodeQuest.removeQuestButtonIfNeeded();
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();
		}
		else if(type.getClass().getName().equals("NodeQuestLog")){
			popOptions.removeAll();
			revalidate();
			nodeQuestLog.showQuestLogLevel(0);
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();
		}*/
		else if(type.getClass().getName().equals("NodeTrainer")){
			popOptions.removeAll();
			imageLabel.setIcon(null);
			revalidate();
			nodeTrainer.show();
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();
		}
		else if(type.getClass().getName().equals("NodeSave")){
			popOptions.removeAll();
			imageLabel.setIcon(null);
			revalidate();
			nodeSave.show();
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();
		}
		else if(type.getClass().getName().equals("NodeExit")){
			popOptions.removeAll();
			imageLabel.setIcon(null);
			revalidate();
			nodeExit.show();
			add(popOptions, new Integer(2));
			frame.getContentPane().repaint();
		}
	}
	
	public void changeImagePic(ImageIcon image){
		imageLabel.setIcon(image);
	}
	
	public void removeOpaqueFromTexteArea(){
		if(node.type.equals(TerrainType.City))
			textArea.setBackground(Color.GRAY);
		else if(node.type.equals(TerrainType.Village))
			textArea.setBackground(new Color(176,224,230));
		else if(node.type.equals(TerrainType.Forest))
			textArea.setBackground(new Color(107,142,35));
		else if(node.type.equals(TerrainType.Field))
			textArea.setBackground(new Color(144,238,144));
		else if(node.type.equals(TerrainType.Mountains))
			textArea.setBackground(Color.GRAY);
		else if(node.type.equals(TerrainType.Sand))
			textArea.setBackground(new Color(176,224,230));
		else if(node.type.equals(TerrainType.Lake))
			textArea.setBackground(new Color(176,224,230));
		
	}
	
	 @Override
     protected void paintComponent(Graphics g) {
         if (!isOpaque() && getBackground().getAlpha() < 255) {
             g.setColor(getBackground());
             g.fillRect(0, 0, getWidth(), getHeight());
         }
         super.paintComponent(g);
     }
	
	private class ButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
					
			if(e.getSource().equals(map)){
				UIManager.put("MenuItem.selectionBackground", new Color(163,184,204));
				UIManager.put("MenuItem.selectionForeground", Color.BLACK);
				UIManager.put("MenuItem.foreground", Color.BLACK);
				frame.swapToWorldMap();
			}
			
			else if(e.getSource().equals(save)){
				removeOpaqueFromTexteArea();
				changeDisplay(nodeSave);
			}
			
			else if(e.getSource().equals(trouble)){
				UIManager.put("MenuItem.selectionBackground", new Color(163,184,204));
				UIManager.put("MenuItem.selectionForeground", Color.BLACK);
				UIManager.put("MenuItem.foreground", Color.BLACK);
				frame.swapToCombatSkeleton(node.getRandomEncounter(null), true, node);
			}
			else if(e.getSource().equals(trainer)){
				removeOpaqueFromTexteArea();
				changeDisplay(nodeTrainer);
			}
			else if(e.getSource().equals(exit)){
				removeOpaqueFromTexteArea();
				changeDisplay(nodeExit);
			}
			else if(e.getSource().equals(backPack)){
				removeOpaqueFromTexteArea();
				changeDisplay(inventory);
				if(lastItem != null)
					lastItem.setForeground(textColor);
				backPack.setForeground(Color.BLUE);
				lastItem = backPack;
				popPrime.show(frame, 50, 55);
			}
			else if(e.getSource().equals(shop)){
				removeOpaqueFromTexteArea();
				changeDisplay(shopInt);
				if(lastItem != null)
					lastItem.setForeground(textColor);
				shop.setForeground(Color.BLUE);
				lastItem = shop;
				popPrime.show(frame, 50, 55);
			}
			else if(e.getSource().equals(quest)){
				removeOpaqueFromTexteArea();
				changeDisplay(nodeQuest);
				if(lastItem != null)
					lastItem.setForeground(textColor);
				quest.setForeground(Color.BLUE);
				lastItem = quest;
				popPrime.show(frame, 50, 55);
			}
			else if(e.getSource().equals(pets)){
				removeOpaqueFromTexteArea();
				changeDisplay(petsInt);
				if(lastItem != null)
					lastItem.setForeground(textColor);
				pets.setForeground(Color.BLUE);
				lastItem = pets;
				popPrime.show(frame, 50, 55);
			}
			else if(e.getSource().equals(abilities)){
				removeOpaqueFromTexteArea();
				changeDisplay(spellBookInt);
				if(lastItem != null)
					lastItem.setForeground(textColor);
				abilities.setForeground(Color.BLUE);
				lastItem = abilities;
				popPrime.show(frame, 50, 55);
			}
			else if(e.getSource().equals(questLog)){
				removeOpaqueFromTexteArea();
				changeDisplay(nodeQuestLog);
				if(lastItem != null)
					lastItem.setForeground(textColor);
				questLog.setForeground(Color.BLUE);
				lastItem = questLog;
				popPrime.show(frame, 50, 55);
			}
			else
				popPrime.show(frame, 50, 55);
		
		}

	}
	
	private class TheMouseListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			popPrime.show(frame,50,55);
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}

		@Override
		public void mousePressed(MouseEvent e) {
			popPrime.show(frame, 50, 55);
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
		}
		
	}
	
	public void addKeyBindings(){
		
		InputMap im = getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
	    ActionMap am = getActionMap();

	    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Escape");	

	    am.put("Escape", new KeyAction("Escape"));
	    
	}
	/*
	public void addQuestLogIfNeeded(){
		if(player.activeQuests.size() > 0 && !hasQuestLog){
			// TODO: Fixa questlog så den lyssnar på ny typ av questlist
			nodeQuestLog = new NodeQuestLog(this.player,this.frame);
			questLog = new JMenuItem("Uppdragslista");
			popPrime.add(questLog, 0);
			questLog.setOpaque(false);
			questLog.setFont(font);
			questLog.addActionListener(new ButtonListener());
			questLog.addMouseListener(new TheMouseListener());
			frame.getContentPane().repaint();
			hasQuestLog = true;
		}
		else if(player.activeQuests.size() == 0 && hasQuestLog)
			popPrime.remove(questLog);
	}*/
	
	private class KeyAction extends AbstractAction{
		

	    private String cmd;

	    KeyAction(String cmd) {
	        this.cmd = cmd;
	    }
		
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			if(cmd.equals("Escape")){
				popPrime.setVisible(false);
				UIManager.put("MenuItem.selectionBackground", new Color(163,184,204));
				UIManager.put("MenuItem.selectionForeground", Color.BLACK);
				frame.swapToWorldMap();
			}			
		}
	}
}
	
